﻿using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MTQ.Web.Controllers
{
    public partial class Base_RoleAuthController : BaseController
    {
        //
        // GET: /Base_RoleAuth/

        public ActionResult Index()
        {
            return View();
        }


        private IBase_AuthService ibase_authService = new Base_AuthService();
        private IBase_RoleAuthService ibase_roleauthService = new Base_RoleAuthService();

        [AccountAuth(Roles = "Base_Role_AuthConfig")]
        public ActionResult AuthConfig(Guid? primarykey)
        {
            ViewBag.RoleId = primarykey;

            return View();
        }

        [HttpPost]
        [AccountAuth(Roles = "Base_Role_EditAuth")]
        public ActionResult EditAuth(Guid primarykeys, string authid)
        {
            List<ModelMessage> listMsg = new List<ModelMessage>();
            // List<string> arrNewAuth = authid.Split(',').ToList();
            List<Guid> newAuthList = authid.Split(',').ToList().ConvertAll<Guid>(x => new Guid(x));
            List<Base_RoleAuth> listRoleAuth = ibase_roleauthService.LoadEntities(x => x.Role_Id == primarykeys).ToList();

            List<Base_RoleAuth> listDeleteId = new List<Base_RoleAuth>();
            List<Base_RoleAuth> listAddId = new List<Base_RoleAuth>();

           //获取删除数据
            foreach (var item in listRoleAuth)
            {
                if (!newAuthList.Contains(item.Auth_Id))
                {
                    listDeleteId.Add(item);
                }
            }

            //获取新增的数据
            List<Guid> arrOldAuth = listRoleAuth.Select(x => x.Auth_Id).ToList();
            foreach (Guid item in newAuthList)
            {
                if (!arrOldAuth.Contains(item))
                {
                    Base_RoleAuth roleauth = new Base_RoleAuth()
                    {
                        RoleAuth_Id = System.Guid.NewGuid(),
                        Auth_Id = item,
                        Role_Id = primarykeys
                    };
                    listAddId.Add(roleauth);
                }

            }


            ibase_roleauthService.AddListEntity(listAddId);
            ibase_roleauthService.DeleteListEntity(listDeleteId);

            listMsg.Add(new ModelMessage("ChangeResult", "m001", "保存成功" ));
            return Json(listMsg);
        }


        [HttpPost]
        [AccountAuth(Roles = "Base_RoleAuth_GetAuth")]
        public ActionResult GetAuth(Guid? primarykeys)
        {
            List<TreeView> listMsg = new List<TreeView>();
            List<Base_Auth> listAllAuth = ibase_authService.LoadEntities(x => true).ToList();
            List<Base_RoleAuth> listRoleAuth = ibase_roleauthService.LoadEntities(x => x.Role_Id == primarykeys).ToList();

            listMsg = GetAuth(listAllAuth.Where(x => x.Auth_Level == 0).ToList(), listAllAuth);
            GetRoleAuth(listRoleAuth, listMsg);

            return Json(listMsg);
        }

        private List<TreeView> GetAuth(List<Base_Auth> listCurrentAuth, List<Base_Auth> listAllAuth)
        {
            if (listCurrentAuth == null || listCurrentAuth.Count == 0)
            {
                return null;
            }
            List<TreeView> listMsg = new List<TreeView>();
            //父节点
            foreach (var item in listCurrentAuth)
            {
                List<Base_Auth> listChildren = listAllAuth.Where(x => x.Auth_Code.StartsWith(item.Auth_Code) && x.Auth_Level == item.Auth_Level + 1).ToList();

                TreeView tv = new TreeView()
                {
                    a_attr = new A_attrs() { Auth_Id = item.Auth_Id },
                    text = item.Auth_Name,
                    children = GetAuth(listChildren, listAllAuth)
                };
                listMsg.Add(tv);
            };

            return listMsg;
        }

        private TreeViewState treeViewStateSelected = new TreeViewState() { selected = true };
        private void GetRoleAuth(List<Base_RoleAuth> listRoleAuth, List<TreeView> listMsg)
        {
            foreach (var item in listMsg)
            {
                if (listRoleAuth.Where(x => x.Auth_Id == item.a_attr.Auth_Id).Count() > 0)
                {
                    item.state = treeViewStateSelected;
                }
                if (item.children != null)
                {
                    GetRoleAuth(listRoleAuth, item.children);
                }
            }
        }

    }
}
